package com.crocodile.game17.handler;

import com.alibaba.fastjson.JSON;
import com.crocodile.game17.common.AppExceptionMsgEnum;
import com.crocodile.game17.common.R;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        String result = JSON.toJSONString(R.error(AppExceptionMsgEnum.UNAUTHORIZED));

        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        response.setStatus(200);

        response.getWriter().print(result);
    }
}
